package com.hengpeng.itfinoss.controller.fund;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.util.StringHelper;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.AccountLog;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.service.ServiceFactory;

@Controller
@RequestMapping("/accountlog")
public class AccountLogController extends BaseController
{
	@Autowired
	private ServiceFactory serviceFactory;

	@RequestMapping(value = "/list")
	public String list(ModelMap map, HttpServletRequest request, String userName, String trustAccountId,
			String startTime, String endTime)
	{
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		Date start = null;
		Date end = null;
		if (!StringHelper.isEmpty(startTime))
		{
			start = StringHelper.getDateFromString(startTime, "yyyy-MM-dd HH:mm");
		}
		if (!StringHelper.isEmpty(endTime))
		{
			end = StringHelper.getDateFromString(endTime, "yyyy-MM-dd HH:mm");
		}
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		List<AccountLog> list = new ArrayList<AccountLog>();
		map.put("userName", userName);
		map.put("trustAccountId", trustAccountId);
		List<AccountLog> result = serviceFactory.getAccountLogService()
				.findWithPage(userName, trustAccountId, start, end, page.currentPage, page.pageSize, null)
				.getReturnValue();
		Long count = serviceFactory.getAccountLogService().queryCount(userName, trustAccountId, start, end)
				.getReturnValue();
		page.setEntityCount(count.intValue());
		list.addAll(result);
		map.put("list", list);
		map.put("page", page);
		return "fund/accountlog/list";
	}

	@RequestMapping(value = "/detail/{id}")
	public String detail(ModelMap map, HttpServletRequest request, @PathVariable Long id)
	{
		AccountLog accountLog = serviceFactory.getPersistService().get(AccountLog.class, id);

		map.put("accountLog", accountLog);
		return "fund/accountlog/detail";
	}
}
